<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

// 使用类

use App\Http\Controllers\Controller;

// 搜索控制器
class SearchController extends Controller
{
    // 分类页

     public function index(Request $request){

        $search = $request -> input('search','');
        $request = $request -> all();

           // 分类
        $types=\DB::table("types")->where('pid',0)->get();

        $goods=\DB::table('goods')
                    ->join('goodsdetails',"goodsdetails.gid","=","goods.id")
                    ->where('is_on_sale',1)
                    ->where("goods.goodsname",'like','%'.$search.'%')
                    ->orWhere("goods.title",'like','%'.$search.'%')
                    ->orWhere("goodsdetails.text",'like','%'.$search.'%')
                    ->get();
    //热卖
     $hotgoods =\DB::table('orders')
                     ->select(\DB::raw('goods.*,orders.gid,COUNT(orders.gid) AS counts'))
                     ->leftJoin('goods', 'goods.id', '=', 'orders.gid')
                     ->groupBy('orders.gid')
                     ->orderBy('counts', 'desc')
                     ->limit(6)
                     ->get();
// dd($goods);
        // 加载页面

       return view("home.search")->with('types',$types)->with('goods',$goods)->with('hotgoods',$hotgoods);

    }




}
